package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.Student;
import com.qf.service.StudentService;
import com.qf.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zdl
 * @date 2025/2/10
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;

    @GetMapping("/allStudent")
    public R<PageInfo<Student>> getAllStudent(@RequestParam int page,@RequestParam int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Student> allStudent = studentService.findAllStudent();
        PageInfo<Student> studentPageInfo = new PageInfo<>(allStudent);
        return R.ok(studentPageInfo);
    }

    @GetMapping("/getStudentById")
    public R<Student> getStudentById(int id) {
        return R.ok(studentService.findStudentById(id));
    }

    @PostMapping("/updateStudent")
    public R<?> updateStudent(@RequestBody Student student) {
        int i = studentService.updateStudentById(student);
        if (i > 0) {
            return R.ok("修改成功");
        }
        return R.fail("5003", "数据修改失败");
    }

    @GetMapping("/like")
    public R<List<Student>> queryLikeSnameOrClassName(String sname, String className) {
        System.out.println(sname);
        System.out.println(className);
        return R.ok(studentService.findStudentLikeSnameOrClassName(sname, className));
    }

    @GetMapping("/del")
    public R<?> delStudentById(int id) {
        return R.ok(studentService.delStudentById(id));
    }
}
